package com.lmc.form.controller;

import com.free.bsf.core.db.DbConn;
import com.free.bsf.core.db.DbHelper;
import com.free.bsf.core.util.ContextUtils;
import com.free.bsf.core.util.JsonUtils;
import com.free.bsf.core.util.LogUtils;
import com.free.bsf.core.util.StringUtils;
import com.lmc.business.api.web.response.ApiResponseEntity;
import com.lmc.form.dao.dal.paas_form_model_dal;
import com.lmc.form.dao.model.auto.paas_form_model_model;
import com.lmc.form.script.DataSourceScripts;
import com.lmc.form.script.DynamicDataSourceScript;
import com.lmc.form.script.DynamicScript;
import com.lmc.form.script.RequestWrapper;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.sql.DataSource;
import java.util.*;

@Slf4j
@Controller
@RequestMapping("/api")
public class ApiController extends SpringMvcController {
    @RequestMapping("/getSubForms/")
    public ModelAndView getSubForms()
    {
        return jsonVisit((m) -> {
            return DbHelper.get(this.getDataSource(),(c)->{
                val os = new paas_form_model_dal().getSubForms(c);
                return os;
            });
        });
    }

    @RequestMapping("/testDataSource/")
    public ModelAndView testDataSource(String form_key,String name, String code, String formData)
    {
        return jsonVisit((m) -> {
            Map dataSourceResult = new HashMap<String,Object>();
            val requestJson =new LinkedHashMap();requestJson.put("form_key",form_key);requestJson.put("custom_json",JsonUtils.deserialize(formData,Object.class));
            val formInfoStr = "{form_key:'"+ code+"'}";
            try {
                val result = DynamicScript.run(formInfoStr, code, requestJson);
                dataSourceResult.put(name, result);
            }catch (Exception e){
                //测试无需打印
                int a=1;
            }
            return dataSourceResult;
        });
    }

    @RequestMapping("/getDataSource/")
    public ModelAndView getDataSource(String form_key, String formData,String dataSourceScripts)
    {
        return jsonVisit((m) -> {
                return DynamicDataSourceScript.run(form_key,JsonUtils.deserialize(formData, Object.class),dataSourceScripts,null);
        });
    }

    @RequestMapping("/saveform/")
    public ModelAndView saveForm(paas_form_model_model model)
    {
        return jsonVisit((m) -> {
            model.id=0;
            return DbHelper.get(this.getDataSource(),(c)->{
                model.create_time= new Date();model.update_time=new Date();
                new paas_form_model_dal().addOrUpdate(c,model);
                return true;
            });
        });
    }

    @RequestMapping("/resetform/")
    public ModelAndView resetForm(String form_key,int version)
    {
        return jsonVisit((m) -> {
            return DbHelper.get(this.getDataSource(),(c)->{
//                new paas_form_model_dal().addOrUpdate(c,form_key);
                return true;
            });
        });
    }

    @RequestMapping("/deleteform/")
    public ModelAndView deleteForm(String form_key)
    {
        return jsonVisit((m) -> {
            return DbHelper.get(this.getDataSource(),(c)->{
                new paas_form_model_dal().deleteByKey(c,form_key);
                return true;
            });
        });
    }

    @RequestMapping("/getform/")
    public ModelAndView getform(String form_key)
    {
        return jsonVisit((m) -> {
            val map = new HashMap<String,Object>();
            val form = DbHelper.get(this.getDataSource(),(c)->{
                paas_form_model_model o = new paas_form_model_dal().get(c,form_key);
                map.put("form",o);
                val os = new paas_form_model_dal().getSubForms(c);
                map.put("subforms",os);
                return o;
            });

            Map ds = new HashMap<>();
            if(form!=null)
            { ds= DynamicDataSourceScript.run(form_key,new ArrayList<Object>(),form.form_design_datasource,"page_preload");}
            map.put("datasourceResults",ds);

            return map;
        });
    }

    @RequestMapping("/buttonclick/")
    public ModelAndView buttonclick(String form_key){
        return jsonVisit((m)->{
            return null;
        });
    }

    @RequestMapping("/test/")
    public ModelAndView test(@RequestBody testInfo model)
    {
        return jsonVisit((m) -> {
           return true;
        });
    }

    @RequestMapping("/test2/")
    public ModelAndView test2()
    {
        return jsonVisit((m) -> {
            return "dasdfasdf";
        });
    }
    @AllArgsConstructor
    @Data
    @NoArgsConstructor
    public static class testInfo{
        private String a;
        private String b;
        private Object json_data;
    }
}
